package com.zzptc.tkfate;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.zzptc.tkfate.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    EditText et_tk_name,et_tk_pwe;
    Switch Switch_tk_login;
    Button but_tk_login;
    boolean flag;
    List<User> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        eventListener();
        User user1=new User("ad","660");
        User user2=new User("cd","880");
        User user3=new User("md","220");
        list.add(user1);
        list.add(user2);
        list.add(user3);

    }
    private void eventListener(){
        Switch_tk_login.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isChecked()) {
                    //Toast.makeText(MainActivity.this, "选择了", Toast.LENGTH_SHORT).show();
                    flag=true;
                }else {
                    //Toast.makeText(MainActivity.this, "没选择", Toast.LENGTH_SHORT).show();
                    flag=false;
                }
            }
        });
        but_tk_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et_tk_name.getText().toString().trim();
                String pwd = et_tk_pwe.getText().toString().trim();
                SharedPreferences sh = getSharedPreferences("one",MODE_PRIVATE);
                SharedPreferences.Editor edit = sh.edit();
                if (flag) {
                    edit.putString("uname",name);
                    edit.putString("password",pwd);
                    edit.putBoolean("autologin",flag);
                    edit.commit();
                }else {
                    edit.putString("uname",name);
                    edit.putString("password",pwd);
                    edit.putBoolean("autologin",false);
                    edit.commit();
                }
                //登录
                login(name,pwd);
            }
        });
    }
    public void login(String uname,String password){
        boolean isLogin=false;
        for(User u:list){
            if(u.getUname().equals(uname)&&u.getPassword().equals(password)){
                Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                isLogin=true;
                break;
            }else{
                isLogin=false;
            }
        }
        if(isLogin){
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void init(){
        et_tk_name=findViewById(R.id.et_tk_name);
        et_tk_pwe=findViewById(R.id.et_tk_pwe);
        Switch_tk_login=findViewById(R.id.Switch_tk_login);
        but_tk_login=findViewById(R.id.but_tk_login);
    }
}